<?php
namespace Amoysea\Controller;
use Think\Controller;
class SystemController extends BaseController {
    public function _initialize()
    {
        parent::_initialize();
        $g=session('company');
		C('erp_dbname',$g['db']);
        $db_name=$g['db'];
    }
	//批量删除
	public function delAll_baseconfig(){
		$ids=I('ids');
		$map['id']=array('in',$ids);
		$m=M('Config', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$status=$m->where($map)->delete();
		if($status){
			$return['status']=1;
			$return['info']="删除成功";
			$this->ajaxReturn($return);
		}else{
			$return['status']=0;
			$return['info']="出错";
			$this->ajaxReturn($return);
		}
	}
	//logo修改
	public function logo(){
		$m=M('User');
		$username=session('company.username');
		$id=session('company.id');
		if(IS_POST){
			$param=I('param.','','trim');
			$data['company']=$param['company_name'];
			if (!empty($_FILES)){
				$config = array(
					'maxSize'    =>    3145728,    
					'savePath'   =>    '/Import/',    
					'saveName'   =>    array('uniqid',''),    
					'exts'       =>    array('jpg','jpeg','png','gif'),    
					'autoSub'    =>    true,    
					'subName'    =>    array('date','Ymd'),);
				$upload = new \Think\Upload($config);// 实例化上传类
				$info   =   $upload->upload();    
				if(!$info) {// 上传错误提示错误信息        
					$this->error($upload->getError());    
				}
				$extensionf=$info['logo']['ext'];
				$logo='./Uploads'.$info['logo']['savepath'].$info['logo']['savename'];
				
				// textgd($file_namef);//加水印
				$data['logo']=$logo;
			}
			$res=$m->where(array('username'=>$username))->save($data);
			if($res!==false){
				$company=$m->where(array('id'=>$id))->getField('company');
				session('company.company',$company);
				$return['status']=1;
				$return['message']='修改成功！';
				$this->ajaxReturn($return);
			}else{
				$return['status']=0;
				$return['message']='修改失败！';
				$this->ajaxReturn($return);
			}
		}else{
			// dump(session());
			$this->display();
		}
	}
	//密码修改
	public function editpwd(){
		if(IS_POST){
			$type=session('company.type');
			$username=session('company.username');
			$salt=session('company.salt');
			$pwd=I('pwd');
			$pwd_c=I('pwd_c');
			if($type==1){
				$m=M('User');
			}else{
				$m=M('Role', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			}
			if($pwd==$pwd_c){
				$data['password']=md5($pwd.$salt);
				$res=$m->where(array('username'=>$username))->save($data);
				if($res!==false){
					$this->success('修改成功！');
				}else{
					$this->error('修改失败！');
				}
			}else{
				$this->error('两次密码输入不一致！');
			}
		}else{
			// dump(session());
			$this->display();
		}
	}
    public function baseconfig(){
        $m=M('Config', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
        if(!empty($keywords)){
			if($selval=='1'){
				$map['name'] = array('like','%'.$keywords.'%');
			}
			if($selval=='2'){
				$map['value'] = array('like','%'.$keywords.'%');
			}
        }
		$count=$m->where($map)->count();
		$list=$m->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出

		$this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
    }
	//添加
	function add_configs(){
			$m=M('Config', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$param = I('param.','','trim');
			$param['add_time']=time();
			$res=$m->add($param);
			if($res){
				$this->success('添加成功');
			}
	}
	//更新
	function add_config(){
		if(IS_POST){
			$m=M('Config', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$param = I('param.','','trim');
			$param['update_time'] = time();
			$id=$param['id'];
			$res=$m->where(array('id'=>$id))->save($param);
			if($res!==false){
				$this->success('更新成功');
			}
		}else{
			$this->display();
		}
	}
	//编辑
	function edit_config(){
		$id=trim(I('id'));
		$m=M('Config', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$detail=$m->where(array('id'=>$id))->find();
		$this->assign('detail',$detail);
		$this->display();
	}
	//删除
	function del_config(){
		$id=trim(I('id'));
		$m=M('Config', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$map['id']=$id;
		$status=$m->where($map)->delete();

		if($status!==false){
            echo "y";
        }else{
            echo "操作失败";
        }
	}
}